var t = require("../../utils/util.js"),
    e = getApp(),
    o = (require("../../utils/function.js"),
        require("../../data/mockData.js"), require("../../api.js"));

wx.getStorageSync("openId");

Page({
    data: {
        isLogin: !1,
        isBind: !1,
        logs: [],
        userInfo: {}
    },
    onLoad: function () {
        wx.getStorageSync("user_info") && this.setData({
            isLogin: !0
        }), this.getAppInfo(), this.setData({
            logs: (wx.getStorageSync("logs") || []).map(function (e) {
                return t.formatTime(new Date(e));
            })
        });
    },
    getAppInfo: function () {
        var t = this;
        e.request({
            url: o.user.payWay,
            method: "post",
            success: function (e) {
                t.setData({
                    info: e.data
                });
            }
        });
    },
    cancel: function () {
        wx.navigateBack({});
    },
    fan: function () {
        wx.navigateBack({
            delta: 1
        });
    },
    onShow: function () {
        var t = this;
        e.request({
            url: o.share.authorization,
            method: "post",
            data: {},
            success: function (e) {
                t.setData({
                    authorize: e.data.image
                });
            }
        });
    },
    toDetail: function (t) {
        var e = t.currentTarget.dataset.url;
        console.log(t), wx.redirectTo({
            url: e
        });
    },
    getUserPhone: function (t) {
        console.log("手机号");
        console.log(t)
        var n = wx.getStorageSync("user_info");
        wx.login({
            success: function (a) {
                e.request({
                    url: o.user.bindPhone,
                    method: "post",
                    data: {
                        code: a.code,
                        iv: t.detail.iv,
                        encryptedData: t.detail.encryptedData
                    },
                    success: function (t) {
                        1 == t.code ? (wx.showToast({
                            title: "绑定成功"
                        }), n.phone = t.data.phone, wx.setStorageSync("user_info", n), setTimeout(function () {
                            wx.navigateBack({});
                        }, 1e3)) : wx.showToast({
                            title: t.msg,
                            icon: "none"
                        });
                    }
                });
            }
        });
    },
    getUserProfile: function (t) {
        var n = this,a = wx.getStorageSync("scene") ? wx.getStorageSync("scene") : "";

        wx.getUserProfile({
            desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
            success: (s) => {

                console.log(s);
              var  siv= s.iv
              var sencryptedData = s.encryptedData
              var avatarUrl = s.userInfo.avatarUrl 
              var gender = s.userInfo.gender
              var nickName = s.userInfo.nickName
                this.setData({
                    userInfo: s.userInfo,
                })
                this.getSettings(), wx.showLoading({}), wx.login({
                    success: function (t) {
                        console.log(t);
                        wx.hideLoading(), e.request({
                            url: o.user.login,
                            method: "post",
                            data: {
                                code: t.code,
                                iv: siv,
                                encryptedData: sencryptedData,
                                id: a,
                                avatarUrl:avatarUrl,
                                gender:gender,
                                nickName:nickName
                            },
                            success: function (t) {
                                console.log(t);
                                if (1 == t.code) {
                                    var o = {};
                                    o.type = "login", o.client_name = t.data.access_token, o.room_id = e.siteInfo.acid,
                                        o.uid = t.data.access_token, wx.sendSocketMessage({
                                            data: JSON.stringify(o)
                                        });
                                    t.data.is_binding;
                                    wx.setStorageSync("user_info", t.data), wx.setStorageSync("uid", t.data.id), "" != t.data.phone && wx.navigateBack({}),
                                        n.setData({
                                            authStatus: !0,
                                            is_GZH: !0,
                                            isLogin: !0
                                        }), wx.setStorageSync("access_token", t.data.access_token);
                                } else wx.removeStorage({
                                    key: "access_token"
                                }), wx.removeStorage({
                                    key: "uid"
                                }), wx.showToast({
                                    title: "登录失败",
                                    icon: "none",
                                    duration: 2e3
                                });
                            }
                        });

                    },
                    fail: function () {
                        wx.hideLoading({});
                    }
                });
            }
        })
    },









    getSettings: function () {
        wx.getSetting({
            success: function (t) {
                console.log(JSON.stringify(t), "初始化进入该页面"), void 0 != t.authSetting["scope.userLocation"] && 1 != t.authSetting["scope.userLocation"] ? wx.showModal({
                    title: "请求授权当前位置",
                    content: "需要获取您的地理位置，请确认授权",
                    success: function (t) {
                        t.cancel ? wx.showToast({
                            title: "拒绝授权",
                            icon: "none",
                            duration: 1e3
                        }) : t.confirm && wx.openSetting({
                            success: function (t) {
                                1 == t.authSetting["scope.userLocation"] ? (wx.showToast({
                                    title: "授权成功",
                                    icon: "success",
                                    duration: 1e3
                                }), wx.getLocation({
                                    success: function (t) {}
                                })) : wx.showToast({
                                    title: "授权失败",
                                    icon: "none",
                                    duration: 1e3
                                });
                            }
                        });
                    }
                }) : (t.authSetting["scope.userLocation"], wx.getLocation({
                    success: function (t) {}
                }));
            }
        });
    }
});